﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.IO;
using System.Drawing;

namespace UbuntuMigration
{
    public class User
    {
        public User(String path)
        {
            this.profiledir = path;
            RegistryKey profilelist = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList");
            getUID(profilelist);
            getProfileImage(path);
            getWallpaper();
            getLocale();
            getUsername();
            getMouseConfig();
            getNetworkShares();
            getPrinters();
            
        }

        private void getPrinters()
        {
            if (Registry.Users.OpenSubKey(this.UID + "\\Printers\\Settings").GetValueNames().Count() > 0)
            {
                this.printers = new List<String>();
                foreach (String prn in Registry.Users.OpenSubKey(this.UID + "\\Printers\\Settings").GetValueNames())
                {
                    this.printers.Add(prn);
                }
            }
        }

        private void getNetworkShares()
        {
            if (Registry.Users.OpenSubKey(this.UID + "\\Network").SubKeyCount > 0)
            {
                this.networkdrives = new Dictionary<String, String>();
                foreach (String letter in Registry.Users.OpenSubKey(this.UID + "\\Network").GetSubKeyNames())
                {
                    String drivepath = (String)Registry.Users.OpenSubKey(this.UID + "\\Network\\" + letter).GetValue("RemotePath");
                    this.networkdrives.Add(letter, drivepath);
                }
            }
        }

        private void getMouseConfig()
        {
            if ((String)Registry.Users.OpenSubKey(this.UID + "\\Control Panel\\Mouse").GetValue("SwapMouseButtons") == "1")
            {
                this.swapmousebuttons = true;
            }
        }

        private void getUsername()
        {
            this.username = (String)Registry.Users.OpenSubKey(this.UID + "\\Volatile Environment").GetValue("USERNAME");
        }

        private void getLocale()
        {
            this.locale = (String)Registry.Users.OpenSubKey(this.UID + "\\Control Panel\\International").GetValue("LocaleName");
        }

        private void getWallpaper()
        {
            this.wallpaper = (String)Registry.Users.OpenSubKey(this.UID + "\\Control Panel\\Desktop").GetValue("Wallpaper");
        }

        private void getProfileImage(String path)
        {
            //Get profileImage
            String imagePath = Path.Combine(path, "AppData\\Local\\Temp", Path.GetFileName(path) + ".bmp");
            if (File.Exists(imagePath))
            {
                this.profileImage = imagePath;
                this.profileImageData = Bitmap.FromFile(imagePath);
            }
        }

        private void getUID(RegistryKey profilelist)
        {
            //Get UID
            foreach (String rkeyname in profilelist.GetSubKeyNames())
            {
                RegistryKey rkey = profilelist.OpenSubKey(rkeyname);
                if (profiledir == (String)rkey.GetValue("ProfileImagePath"))
                {
                    UID = rkeyname;
                }
            }
        }
        public String profiledir;
        public String UID;
        public String profileImage;
        public Image profileImageData;
        public String wallpaper;
        public String locale;
        public Boolean swapmousebuttons = false;
        public Dictionary<String,String> networkdrives;
        public List<String> printers;
        public String username;
        public Boolean selected = false;
    }
}
